//
//  Compilation.m
//  Map
//
//  Created by peoit-xq on 15/12/28.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "MapMananger.h"
#import "MapModel.h"
@interface MapMananger()<AMapSearchDelegate>{

    AMapSearchAPI *_searchAPI;
    AMapPOIKeywordsSearchRequest *_poiRequest;
    NSMutableArray *_arrayResult;

    //反编译
    MapCompilationWithLocationBlock reGeocodeCompletionBlock;
    //地理编码
    MapCompilationWithAddressBlock geocodeCompletionBlock;
    //关键字搜索
    MapPOIKeywordsSearchBlock poiKeyWordsSearchBlock;
}

@end

@implementation MapMananger

- (instancetype)init{

    self = [super init];
    if (self) {
        if (!_searchAPI) {
            [AMapSearchServices sharedServices].apiKey = (NSString *)APIKey;
            _searchAPI = [[AMapSearchAPI alloc] init];
            _searchAPI.delegate = self;
            _searchAPI.language = AMapSearchLanguageZhCN;
        }
        
    }
    return self;
}

//反编译
- (void)AntiCompilationWithLocation:(CLLocation *)location{

    AMapReGeocodeSearchRequest *regeoRequest = [[AMapReGeocodeSearchRequest alloc] init];
    float latitude = location.coordinate.latitude;
    float longitude = location.coordinate.longitude;
    regeoRequest.location = [AMapGeoPoint  locationWithLatitude:latitude longitude:longitude];
    regeoRequest.radius = 1500;
    regeoRequest.requireExtension = YES;
    
    //发起逆地理编码
    [_searchAPI AMapReGoecodeSearch: regeoRequest];
}

//反编译 block
- (void)AntiCompilationWithLocation:(CLLocation *)location completion:(MapCompilationWithLocationBlock)completion {
    
    reGeocodeCompletionBlock = completion;
    [self AntiCompilationWithLocation:location];
    
}
//实现逆地理编码的回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    //根据经纬度会输出该经纬度下的详细地址 国家 地区 街道 之类的
    if (response.regeocode != nil)
    {
        NSString *result;
        if (response.regeocode.addressComponent !=nil)
        {
            AMapGeoPoint *point;
            if (response.regeocode.pois.count > 0)
            {
                AMapPOI *poi = response.regeocode.pois[0];
                point = poi.location;
                result = poi.name;
            }
           
            NSString *address = [NSString stringWithFormat:@"%@%@%@%@%@",response.regeocode.addressComponent.city,response.regeocode.addressComponent.district,response.regeocode.addressComponent.township,response.regeocode.addressComponent.neighborhood,response.regeocode.addressComponent.building];
            NSLog(@"%@--",address);
            if (reGeocodeCompletionBlock) {
                
                reGeocodeCompletionBlock(address);
            }else{
                
                if (self.delegate &&
                    [self.delegate respondsToSelector:@selector(compilation:returnAntiCompilationResult:)]) {
                    
                    [self.delegate compilation:self returnAntiCompilationResult:address];
                }
            }
        }
    }
}
//地理编码
- (void)CompilationWithAddress:(NSString *)address{
    
    //构造AMapGeocodeSearchRequest对象
    AMapGeocodeSearchRequest *geoRequest = [[AMapGeocodeSearchRequest alloc] init];
    geoRequest.city = !self.userCity?@"":self.userCity;
    geoRequest.address = address;
    //发起正向地理编码
    [_searchAPI AMapGeocodeSearch: geoRequest];

}

/**
 *  地理编码block
 */
- (void)CompilationWithLocation:(NSString *)address completion:(MapCompilationWithAddressBlock)completion{

    geocodeCompletionBlock = completion;
    [self CompilationWithAddress:address];
}

//实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    NSLog(@"gecode delegate");
    if(response.geocodes.count == 0)
    {
        //@"无当前搜索地址具体信息，请选择其他地址";
        return;
    }
    //通过AMapGeocodeSearchResponse对象处理搜索结果
    for (AMapGeocode *p in response.geocodes) {
        
        NSLog(@"%@",p);
        if (p.location.latitude>0 && p.location.latitude<90 && p.location.latitude>0 && p.location.longitude<180) {
            //输出编译的经纬度
            CLLocation *loc = [[CLLocation alloc]initWithLatitude:p.location.latitude longitude:p.location.longitude];
            if (geocodeCompletionBlock) {
                
                geocodeCompletionBlock(loc);
            }else{
                
                if (self.delegate &&
                    [self.delegate respondsToSelector:@selector(compilation:returnCompilationResult:)]) {
                    
                    [self.delegate compilation:self returnCompilationResult:loc];
                }
            }
        }
    }
    
}


- (void)POIKeywordsSearchWithString:(NSString *)string{

    _arrayResult = [NSMutableArray array];
    //构造AMapPlaceSearchRequest对象，配置关键字搜索参数
    _poiRequest = [[AMapPOIKeywordsSearchRequest alloc] init];
    _poiRequest.requireExtension = YES;
    _poiRequest.keywords = string;
    _poiRequest.city = !self.userCity?@"":self.userCity;
      //发起POI搜索
    [_searchAPI AMapPOIKeywordsSearch:_poiRequest];

}

/**
*  关键字搜索block
*/
- (void)POIKeywordsSearchWithString:(NSString *)string completion:(MapPOIKeywordsSearchBlock)completion{

    poiKeyWordsSearchBlock = completion;
    [self POIKeywordsSearchWithString:string];
    
}

//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    
    if (response.pois.count==0) {
        [_arrayResult removeAllObjects];
        return;
    }
    for (AMapPOI *p in response.pois) {

        NSString *ads;
        if (p.province.length==0) {
            ads = p.city;
        }else{
            ads = p.province;
        }
        NSLog(@"%@",self.userCity);
        NSString *uloc = !self.userCity?@"":self.userCity;

        if ([ads isEqualToString:uloc] || [ads rangeOfString:uloc].location!=NSNotFound) {
            
            if (p.name.length!=0 && p.address.length!=0) {

                MapModel *model = [[MapModel alloc]init];
                model.mCity = p.city;
                model.mLatitude = p.location.latitude;
                model.mLongitude = p.location.longitude;
                model.mAddress = [NSString stringWithFormat:@"%@%@",p.district,p.address];
                [_arrayResult addObject:model];
                
            }
        }
        
    }
    
    if (poiKeyWordsSearchBlock) {
        poiKeyWordsSearchBlock(_arrayResult);
    }
    
}


//请求错误
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    NSLog(@"error：%@",error);
}

@end
